import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.bytime.toss.common.misc.StringUtil;
import com.bytime.uams.exception.SystemException;
import com.bytime.uams.permissionmanage.enti.CfunModule;


public class TestSqlConnection {

	
	public static void  main(String[]  a)
	{
		
		TestSqlConnection t = new TestSqlConnection();
//		t.testSql("");
//		if (true)
//			return;
		
		Connection connection = null;
	    try {
	        String driverName = "oracle.jdbc.driver.OracleDriver"; // NetDirect JDBC driver
	        String url = "jdbc:oracle:thin:@slh:1521:db" ; // a JDBC url
	        String username = "qhrp";
	        String password = "qhrp";
	    
	        // Load the JDBC driver
	        Class.forName(driverName);
	    
	        // Create a connection to the database
	        connection = DriverManager.getConnection(url, username, password);
	        
	        
	        
	        
	        System.out.println(connection);
	    } catch (ClassNotFoundException e) {
	        // Could not find the database driver
	    	e.printStackTrace();
	    } catch (SQLException e) {
	        // Could not connect to the database
	    	e.printStackTrace();
	    }

	}
	
	
	
	public List testSql(String sql)
			throws SystemException
	{

		sql = "select real_name from rp_user where  login_name = '2'";
		ArrayList lstRslt = new ArrayList();

		Connection con = null;
		Statement stmt = null;
		ResultSet set = null;

		PreparedStatement stmtServ = null;
		ResultSet setServ = null;

		try
		{

			con = getConnection(); // �����ݿ�l��

			stmt = con.createStatement();
			set = stmt.executeQuery(sql);
			while (set.next())
			{
				String strRole = set.getString(1);
				System.out.println(strRole);
				String[] arrRole = splitString(strRole, ",");
				for (int i = 0; i < arrRole.length; i++)
				{
					lstRslt.add(arrRole[i]);
					System.out.println(arrRole[i]);
				}
			}

		} catch (Exception e)
		{
			e.printStackTrace();

		} finally
		{
			try
			{
				if (stmtServ != null)
				{
					stmtServ.close();
				}
				if (con != null)
				{
					con.close();
				}
			} catch (SQLException ex)
			{
				ex.printStackTrace();
			}
		}
		return lstRslt;

	}
	
	public  String[] splitString(String src, String delim) {
		String[] valueArray = null;

		String strtmp = new String();
		Vector vtmp = new Vector();
		int i = 0;
		while (i != -1) {
			i = src.indexOf(delim);
			if (i != -1) {
				strtmp = src.substring(0, i);
			} else {
				strtmp = src;

			}

			vtmp.add(strtmp);

			if (src.equals("")) {
				break;
			}

			if (i != -1) {
				src = src.substring(i + delim.length(), src.length());
			}
		}

		if (vtmp.size() != 0) {
			valueArray = new String[vtmp.size()];
			vtmp.copyInto(valueArray);
		}
		return valueArray;
	}


	private Connection getConnection() throws Exception
	{
		
		Connection connection = null;
	    try {
	        String driverName = "oracle.jdbc.driver.OracleDriver"; // NetDirect JDBC driver
	        String url = "jdbc:oracle:thin:@192.168.0.168:1521:qhrp" ; // a JDBC url
	        String username = "qhrp";
	        String password = "qhrp";
	    
	        // Load the JDBC driver
	        Class.forName(driverName);
	    
	        // Create a connection to the database
	        connection = DriverManager.getConnection(url, username, password);
	        
	        System.out.println(connection);
	    } catch (ClassNotFoundException e) {
	        // Could not find the database driver
	    	e.printStackTrace();
	    } catch (SQLException e) {
	        // Could not connect to the database
	    	e.printStackTrace();
	    }		
	    return connection;
	}

	// session.setAttribute("roleList", lstRslt);
	// Object obj = session.getAttribute("roleList");
	// List roleList = (List)obj;
	// reportName
	// roleList.contains(reportName);


}
